<template>
  <div class="app-container">
    <!--    0人工审核中，1审核通过，-1不通过，2未发布，3机器审核中-->
    <el-tabs v-model="status" @tab-click="handleClick">
      <el-tab-pane label="全部" name="-10"></el-tab-pane>
      <el-tab-pane label="人工审核中" name="0"></el-tab-pane>
      <el-tab-pane label="审核通过" name="1"></el-tab-pane>
      <el-tab-pane label="审核失败" name="-1"></el-tab-pane>
      <el-tab-pane label="未发布" name="2"></el-tab-pane>
      <el-tab-pane label="机器审核中" name="3"></el-tab-pane>
      <el-tab-pane label="已删除" name="4"></el-tab-pane>
    </el-tabs>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="用户id" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column label="内容" width="150">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column label="图片" width="200" align="center">
        <template slot-scope="scope">
          <div v-for="(value, key, index) in scope.row.pic" :key="index">
            <el-image
              style="width: 100px; height: 100px"
              :src="value"
              :preview-src-list="scope.row.pic">
            </el-image>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="发布人" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column v-if="status != 4" class-name="status-col" label="审核状态" width="110" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.status===0">
            <el-tag>人工审核中</el-tag>
          </div>
          <div v-if="scope.row.status===1">
            <el-tag>审核通过</el-tag>
          </div>
          <div v-if="scope.row.status===-1">
            <el-tag>审核不通过</el-tag>
          </div>
          <div v-if="scope.row.status===2">
            <el-tag>未发布</el-tag>
          </div>
          <div v-if="scope.row.status===3">
            <el-tag>机器审核中</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="status==-1" label="失败原因" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.circle.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.time }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="status != 4"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click=" handelDelete(scope.row.id)"></el-button>
          <el-button
            v-if="status == 4"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click=" handelRealDelete(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData">
    </el-pagination>
  </div>
</template>

<script>
import { getList, deleteById, realDeleteById } from '@/api/dynamic'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      status: '-10',
      page: 1,
      size: 10,
      total: 0
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handleClick(tab, event) {
      this.fetchData(this.page)
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      getList({ page: this.page, size: this.size, status: this.status }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    },
    handelDelete(id) {
      this.$confirm('此操作将删除该动态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handelRealDelete(id) {
      this.$confirm('此操作将彻底删除该动态,请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        realDeleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
